﻿using ProgrammingEducationSystem.Models;
using ProgrammingEducationSystem.Repository.IRepository;
using ProgrammingEducationSystem.Services.IServices;
using ProgrammingEducationSystem.Tools;

namespace ProgrammingEducationSystem.Services
{
    public class ClassRoomsService : IClassRoomsService
    {
        private readonly IClassRoomRepository _classRoomRepository;
        private readonly ICourseHourRepository _courseHourRepository;

        public ClassRoomsService(IClassRoomRepository classRoomRepository, ICourseHourRepository courseHourRepository)
        {
            _classRoomRepository = classRoomRepository;
            _courseHourRepository = courseHourRepository;
        }
        public async Task<ClassRoom?> GetById(Guid id) {
            return await _classRoomRepository.FindAsync(id);
        }
        public async Task AddAsync(ClassRoom classRoom)
        {
            await _classRoomRepository.AddAsync(classRoom);
        }

        public async Task<bool> DeleteAsync(Guid classRoomId)
        {
            //判断教室是否有未上完的课课
            var chs = await _courseHourRepository.GetByClassRoomIdAsync(classRoomId);
            chs = chs.Where(ch => !ClassRoomTimeTools.IsExpired(ch.ClassTime)).ToList();
            if (chs != null && chs.Count() != 0) 
            {
                Console.WriteLine("教室ID:"+classRoomId);
                Console.WriteLine("该教室还有课程没有上完");
                return false;
            }
            //查询并删除教室
            var classRoom = await _classRoomRepository.FindAsync(classRoomId);
            if (classRoom == null) {
                Console.WriteLine("找不到教室"+classRoomId);
            }
            _classRoomRepository.Delete(classRoom);
            return true;
        }

        public async Task<bool> HaveLeisureAsync(Guid classRoomId, string date)
        {
            //找出该教室的所有课程
            var chs = await _courseHourRepository.GetByClassRoomIdAsync(classRoomId);
            //判断时间是否冲突
            return chs.Any(ch =>ClassRoomTimeTools.ToDateTime(ch.ClassTime) == ClassRoomTimeTools.ToDateTime(date));
        }

        public async Task<List<ClassRoom>> HaveLeisureAsync(string date)
        {
            List<ClassRoom> reCrs = new List<ClassRoom>();
            //找出所有教室
            var crs = await _classRoomRepository.FindAllAsync();
            foreach (var cr in crs)
            {
                //找出该教室的所有课时
                var chs = await _courseHourRepository.GetByClassRoomIdAsync(cr.Id);
                //如果该教室当前没有课时
                if (!chs.Any(ch => ClassRoomTimeTools.ToDateTime(ch.ClassTime) == ClassRoomTimeTools.ToDateTime(date))) 
                {
                    reCrs.Add(cr);
                }
            }
            return reCrs;
        }

        public async Task<bool> SaveAsync()
        {
            return await _classRoomRepository.SaveAsync();
        }

        public void UpdateAsync(ClassRoom classRoom)
        {
            _classRoomRepository.Update(classRoom);
        }

        public async Task<List<ClassRoom>?> AllAsync()
        {
            return await _classRoomRepository.FindAllAsync();
        }
    }
}
